package com.ship.dispatch.bpm.task.listener;

import com.ship.dispatch.bean.SpDispatchSchemeExecute;
import com.ship.dispatch.bean.SpFobSublease;
import com.ship.dispatch.bpm.utils.ApplicationContextUtil;
import com.ship.dispatch.service.SpArrangeSchemeService;
import com.ship.dispatch.service.SpDispatchSchemeExecuteService;
import com.ship.dispatch.service.SpFobSubleaseService;
import com.ship.dispatch.service.impl.SpArrangeSchemeServiceImpl;
import com.ship.dispatch.service.impl.SpDispatchSchemeExecuteServiceImpl;
import com.ship.dispatch.service.impl.SpFobSubleaseServiceImpl;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 *@title BpmEndListener
 *@description 流程结束监听
 *@author yl
 *@create 2023/12/13 0013
 */
@Component
public class BpmEndListener implements ExecutionListener {

    SpDispatchSchemeExecuteService spDispatchSchemeExecuteService;

    SpFobSubleaseService spFobSubleaseService;

    SpArrangeSchemeService spArrangeSchemeService;

    @Override
    public void notify(DelegateExecution delegateExecution) {
        spDispatchSchemeExecuteService = ApplicationContextUtil.getBean(SpDispatchSchemeExecuteServiceImpl.class);
        SpDispatchSchemeExecute execute = spDispatchSchemeExecuteService.getExceuteByInstanceId(delegateExecution.getProcessInstanceId());
        if(execute!=null){
            execute.setFinishTime(new Date());
            execute.setStatus(1);
            execute.setExecuteStatus(3);
            spDispatchSchemeExecuteService.updateById(execute);
            spArrangeSchemeService = ApplicationContextUtil.getBean(SpArrangeSchemeServiceImpl.class);
            spArrangeSchemeService.getArrangeSchemeChart(execute.getArrangeSchemeDetailId(),3);
        }else {
            spFobSubleaseService = ApplicationContextUtil.getBean(SpFobSubleaseServiceImpl.class);
            SpFobSublease spFobSublease = spFobSubleaseService.getExceuteByInstanceId(delegateExecution.getProcessInstanceId());
            if(spFobSublease!=null){
                spFobSublease.setFinishTime(new Date());
                spFobSublease.setStatus(1);
                spFobSubleaseService.updateById(spFobSublease);
            }
        }
    }
}
